<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/12/12
 * Time: 13:51
 */
namespace App\Services\BackendApi;

use App\Repositories\UserLevelRelationsRepository;
use App\Repositories\UserRepository;

class InvitedService extends CommonService
{
    protected $userRepository;
    protected $userLevelRelationsRepository;

    public function __construct(UserRepository $userRepository,
                                UserLevelRelationsRepository $userLevelRelationsRepository)
    {
        parent::__construct();
        $this->userRepository = $userRepository;
        $this->userLevelRelationsRepository = $userLevelRelationsRepository;
    }

    /**
     * 被邀请人列表
     * @param $userId
     * @param $level
     * @return \Illuminate\Http\JsonResponse
     */
    public function directBeInvited($userId, $level, $page)
    {
        $ownRelations = $this->userLevelRelationsRepository->first($userId);
        if (0 === $level) {
            //直接邀请
            $directInvited = $this->userLevelRelationsRepository->getUserLevelRelationsByPidPaginate($ownRelations->id, $page, 10);
        } else {
            //直接邀请
            $directInvited = $this->userLevelRelationsRepository->getUserLevelRelationsByPidAndLevelPaginate($ownRelations->id, $level, $page, 10);
        }
        $data = [];
        if (!empty($directInvited)) {
            foreach ($directInvited as $key=>$value) {
                $data[$key] = $value;
                $beInvitedUser = $this->userRepository->byId($value['user_id']);
                $data[$key]['realname'] = isset($beInvitedUser->realname) ? hiddenRealName($beInvitedUser->realname) : '';
                $data[$key]['mobile'] = isset($beInvitedUser->mobile) ? hiddenMobile($beInvitedUser->mobile) : '';
                $data[$key]['head_pic'] = isset($beInvitedUser->head_pic) ? getOssImageUrl($beInvitedUser->head_pic) : '';
            }
        }
        return $this->responseJson(0, $data);
    }


}